package gaode.com.recentlywebrtcdemo;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    RtcService mService;
    private RtcServiceConnection rtcConnection;
    Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rtcConnection=new RtcServiceConnection();
         intent = new Intent(this, RtcService.class);
        Button bt= (Button) super.findViewById(R.id.start);
         Button unbind= (Button) super.findViewById(R.id.unbind);
        Button bind= (Button) super.findViewById(R.id.bind);
        Button start= (Button) super.findViewById(R.id.startS);
        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startService(intent);
            }
        });
        Button stop= (Button) super.findViewById(R.id.stopS);
        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                stopService(intent);
            }
        });
        unbind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                unbindService(rtcConnection);
            }
        });
        bind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                bindService(intent, rtcConnection, Context.BIND_AUTO_CREATE);
            }
        });
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("zzw","mService"+mService);
                int num= mService.getRandomNumber();
                Log.i("zzw","num"+num);
            }
        });
    }
    public  class RtcServiceConnection  implements  ServiceConnection{

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            RtcService.LocalBinder binder = (RtcService.LocalBinder) service;
            mService = binder.getService();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mService=null;
        }
    }
}
